package service

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"restfulApi/pkg"
)

func Upload(c *gin.Context) {
	file, _ := c.FormFile("image")
	log.Println(file.Filename)

	dst := uuid.NewString() + ".jpg"
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, "./pkg/"+dst)

	getwd, err := os.Getwd()
	if err != nil {
		return
	}

	filePath := getwd + "./pkg/" + dst

	if file.Size > 1024*1024*5 {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "单个视频文件最大支持5GB",
		})
		return
	}

	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" && ext != ".avi" && ext != ".mkv" && ext != ".jpg" {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "文件限制.mp4、.avi、.mkv格式",
		})
		return
	}

	upload, err := pkg.Upload(filePath)
	if err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "上传失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"Data": gin.H{
			"Upload": upload,
		},
	})

}
